/*
 * Merger.cpp
 *
 *  Created on: 2010-9-14
 *      Author: wenjian
 */

#include "Merger.h"
#include <globes.h>

namespace S {
  unsigned long Merger::ClassId = Thk::hashCode("S::Merger");

  Merger::Merger():Shape(0, 0, 0, 0) {

  }

  Merger::~Merger() {

  }

  void Merger::createPath() {
    shapes.createBoundRect();
    const Gdiplus::RectF* br = shapes.getBoundRect();
    //const Gdiplus::RectF* br = getBoundRect();
//    _x = br->X;
//    _y = br->Y;
    setCoordinate(br->X, br->Y);
    setWidth(br->Width);
    setHeight(br->Height);
    createBoundRect();

    //构建控制点数据
    if(isSelected()){
      buildControlPoint();
    }

    setModified(true);
  }

  void Merger::draw(Gdiplus::Graphics* g) {
    shapes.draw(g);
//    Shape::draw(g);
  }

//  inline const Gdiplus::RectF* Merger::getBoundRect() {
//    //return Ed::Elements::getBoundRect(adjustVal+4);
//    return Ed::Elements::getBoundRect();
//  }

  /**
   * 检查是否包含点(x, y)
   */
  inline bool Merger::intersects(real x, real y){
    if(shapes.intersects(x, y)){
      const std::vector<Ed::Element*>* els = shapes.getElements();
      std::vector<Ed::Element*>::const_iterator it = els->begin();
      for(; it != els->end(); it++){
        if(*it && (*it)->intersects(x, y)){
          return true;
        }
      }
    }
    return false;
  }

  /**
   * 检查是否与元素e相交
   */
  inline bool Merger::intersects(Ed::Element* e){
    const vector<Ed::Element*>* els = shapes.getElements();
    vector<Ed::Element*>::const_iterator it = els->begin();
    for(; it != els->end(); it++){
      if((*it)->intersects(e)){
        return true;
      }
    }
    return false;
  }

  /**
   * 改变大小
   */
  void Merger::adjustSize(real dx, real dy, int cpIndex){
    const std::vector<Ed::Element*>* els = shapes.getElements();
    std::vector<Ed::Element*>::const_iterator it = els->begin();
    for(; it != els->end(); it++){
      Shape* s = (Shape*)*it;
      //s->setCoordinate(s->x()+dx, s->y()+dy);
//      real ow = s->width();
//      real oh = s->height();
      s->adjustSize(dx, dy, cpIndex);
//      real w = s->width();
//      real h = s->height();
//      cout<<"w/ow is "<<w/ow<<endl;
//      cout<<"h/oh is "<<h/oh<<endl;
      //s->setCoordinate(x()+(s->x()-x())*(w/ow), y()+(s->y()-y())*(h/oh));
//      s->setCoordinate(x()+(s->x()-x())*(w/ow), y()+(s->y()-y())*(h/oh));

    }
//    Shape::adjustSize(dx, dy, cpIndex);
    createPath();
  }

  void Merger::addShapes(const std::vector<AbstractElement*>* els){
    shapes.addAll(els);
  }

  /**
   * 增量移动到目标位置
   */
  void Merger::moveBy(real dx, real dy){
    shapes.moveBy(dx, dy);
    createPath();
  }
}
